﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for ArticleBusiness
/// </summary>
public class ArticleBusiness
{
    ArticleData article = new ArticleData();
	public ArticleBusiness()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public bool add(ArticlesEntities item)
    {
        return article.add(item);
    }
    public bool edit(ArticlesEntities item)
    {
        return article.edit(item);
    }
    public bool updateVisited(int id)
    {
        return article.updateVisited(id);
    }
    public bool delete(int id)
    {
        return article.delete(id);
    }

    public ArticlesEntities getItem(int id)
    {
        ArticlesEntities item = null;
        DataTable dt = article.getArticle(id);
        if (dt != null && dt.Rows.Count > 0)
        {
            DataRow row = dt.Rows[0];
            item = new ArticlesEntities();
            item._articlesID = int.Parse(row["id"].ToString());
            item._categoryID = int.Parse(row["category_id"].ToString());
            item._name = row["name"].ToString();
            item._visited = int.Parse(row["visited"].ToString());
            item._image = row["image"].ToString();
            item._summary = row["summary"].ToString();
            item._content = row["content"].ToString();
            item._status = row["status"].ToString();
            item._author = row["author"].ToString();
            item._createDate = row["create_date"].ToString();
        }
        return item;
    }

    private System.Collections.ArrayList get(DataTable dt)
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        if (dt != null && dt.Rows.Count>0)
        {
            ArticlesEntities item = null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                item = new ArticlesEntities();
                DataRow row = dt.Rows[i];
                item._articlesID = int.Parse(row["id"].ToString());
                item._categoryID = int.Parse(row["category_id"].ToString());
                item._name = row["name"].ToString();
                item._visited = int.Parse(row["visited"].ToString());
                item._image = row["image"].ToString();
                item._summary = row["summary"].ToString();
                item._content = row["content"].ToString();
                item._status = row["status"].ToString();
                item._author = row["author"].ToString();
                item._createDate = row["create_date"].ToString();
                items.Add(item);
                item = null;
            }
        }
        return items;
    }
    public System.Collections.ArrayList getArticle()
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        DataTable dt = article.getArticle(0);
        return this.get(dt);
    }

    public System.Collections.ArrayList getArticle_CategoryView(int category_id)
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        System.Collections.ArrayList tmp = this.getArticle();
        if (tmp != null && tmp.Count>0)
        {
            ArticlesEntities item = null;
            for (int i = 0; i < tmp.Count; i++)
            {
                item = (ArticlesEntities)tmp[i];
                if (item._categoryID == category_id && (item._status.Trim().Equals("True") || item._status.Trim().Equals("1")))
                {
                    items.Add(item);
                }
                item = null;
            }
        }
        return items;
    }
    //bai viet trong panel
    public System.Collections.ArrayList getArticle_panel(int panel_id)
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        DataTable dt = article.getArticle_panel(panel_id);
        return this.get(dt);
    }
    //bai viet duoc hien thi trong panel
    public System.Collections.ArrayList viewArticle_panel(int panel_id)
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        System.Collections.ArrayList tmp = this.getArticle_panel(panel_id);
        if (tmp != null && tmp.Count > 0)
        {
            ArticlesEntities item = null;
            for (int i = 0; i < tmp.Count; i++)
            {
                item = (ArticlesEntities)tmp[i];
                if (item._status.Trim().Equals("True") || item._status.Trim().Equals("1"))
                {
                    items.Add(item);
                }
                item = null;
            }
        }
        return items;
    }

    public System.Collections.ArrayList viewImage()
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        System.Collections.ArrayList tmp = this.getArticle();
        if (tmp != null && tmp.Count > 0)
        {
            ArticlesEntities item = null;
            ArticlesEntities it = null;
            for (int i = 0; i < tmp.Count; i++)
            {
                item = (ArticlesEntities)tmp[i];
                if ((item._status.Trim().Equals("True") || item._status.Trim().Equals("1")) && (!item._image.Trim().Equals("")))
                {
                    it = new ArticlesEntities();
                    it._articlesID = item._articlesID;
                    it._image = item._image;
                    items.Add(it);
                    it = null;
                }
                item = null;
            }
        }
        return items;
    }

     public System.Collections.ArrayList getListArticle(int panel_id,string create_date,string file)
        {
            System.Collections.ArrayList items = new System.Collections.ArrayList();
            DataTable dt = article.getListArticle(panel_id, create_date, file);
            return this.get(dt);
        }
}
